package com.yum.Iterator.Set.HashSet;

import com.yum.Iterator.Map.YAbstractMap.HashMap.YHashMap;
import com.yum.Iterator.Set.YAbstractSet;
import com.yum.Iterator.Set.YSet;

import java.io.Serializable;
import java.util.Iterator;

/**
 * Created on 2023/6/12
 *
 * @Time 19:48
 * @Program YunMap
 * @Author YoHaNe/XiaoShuai
 */
public class YHashSet<E> extends YAbstractSet<E>
        implements YSet<E>,Cloneable, Serializable {

    private static final long serialVersionUID = 6818262972982263799L;

    /**
     * 用hashMap存储所有元素
     */
    private transient YHashMap<E,Object> map;

    /**
     * 定义一个虚拟的Object对象作为HashMap的value
     */
    private static final Object PRESENT = new Object();

    public YHashSet(){
        map = new YHashMap<>();
    }

    public YHashSet(int initialCapacity){
        map = new YHashMap<>(initialCapacity);
    }


    public YHashSet(int initialCapacity,float loadFactory){
        map = new YHashMap<>(initialCapacity,loadFactory);
    }

    @Override
    public void clear() {
        map.clear();
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean remove(Object o) {
        return map.remove((E) o) == PRESENT;
    }

    @Override
    public boolean isEmpty() {
        return map.isEmpty();
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean contains(Object o) {
        return map.containsKey((E) o);
    }

    @Override
    public int size() {
        return map.size();
    }

    @Override
    public boolean add(E e) {
        return map.put(e,PRESENT) == null;
    }

    @Override
    public Iterator<E> iterator() {
        return null;
    }
}
